package cn.jbt.api.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.jbt.api.admin.annotation.Log;
import cn.jbt.api.admin.entity.SysUser;
import cn.jbt.api.admin.entity.dto.ModifyPassDTO;
import cn.jbt.api.admin.entity.dto.ModifyUserDTO;
import cn.jbt.api.admin.entity.dto.ResetPassDTO;
import cn.jbt.api.admin.service.ISysUserService;
import cn.jbt.common.PageQryParam;
import cn.jbt.common.PageQryResult;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/user")
@Slf4j
@AllArgsConstructor
public class SysUserController {

    private final ISysUserService iSysUserService;

    @PostMapping("/list")
    @SaCheckPermission("system:user:list")
    @Log(bizType = "查询", bizName = "分页查询用户信息")
    public PageQryResult<SysUser> getAllByPage(@Valid @RequestBody PageQryParam<SysUser> pageQryParam) {
        return iSysUserService.getAllByPage(pageQryParam);
    }

    @PostMapping("/add")
    @SaCheckPermission("system:user:add")
    @Log(bizType = "新增", bizName = "新增用户信息")
    public SysUser add(@Valid @RequestBody SysUser sysUser) {
        return iSysUserService.add(sysUser);
    }

    @PostMapping("/edit")
    @SaCheckPermission("system:user:edit")
    @Log(bizType = "编辑", bizName = "编辑用户信息")
    public SysUser edit(@Valid @RequestBody SysUser sysUser) {
        return iSysUserService.edit(sysUser);
    }

    @PostMapping("/del")
    @SaCheckPermission("system:user:del")
    @Log(bizType = "删除", bizName = "删除用户信息")
    public void del(@NotEmpty @RequestBody List<Long> ids) {
        iSysUserService.del(ids);
    }

    @PostMapping("/resetPass")
    @SaCheckPermission("system:user:resetPass")
    @Log(bizType = "编辑", bizName = "重置用户密码")
    public void resetPass(@Valid @RequestBody ResetPassDTO resetPassDTO) {
        iSysUserService.resetPass(resetPassDTO);
    }

    @PostMapping("/modifyPass")
    @SaCheckPermission("system:user:modifyPass")
    @Log(bizType = "编辑", bizName = "修改用户密码")
    public void modifyPass(@Valid @RequestBody ModifyPassDTO modifyPassDTO) {
        iSysUserService.modifyPass(modifyPassDTO);
    }

    @PostMapping("/updateAvatar")
    @SaCheckPermission("system:user:updateAvatar")
    @Log(bizType = "编辑", bizName = "更新用户头像")
    public void updateAvatar(@RequestParam String avatar) {
        iSysUserService.updateAvatar(avatar);
    }

    @PostMapping("/modifyUser")
    @SaCheckPermission("system:user:modifyUser")
    @Log(bizType = "编辑", bizName = "修改用户基本资料")
    public void modifyUser(@Valid @RequestBody ModifyUserDTO modifyUserDTO) {
        iSysUserService.modifyUser(modifyUserDTO);
    }
}
